package com.jvm.classloader.jdbc;

import java.sql.Driver;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * App..
 *
 * @author Lizhong
 * @date 2019/7/24
 */
public class App {
    public static void main(String[] args) {
        // testContextClassLoader();
        method01(Thread.currentThread().getContextClassLoader());
    }
    public static void testContextClassLoader() {
        //获取extclassloader

        System.out.println(ClassLoader.getSystemClassLoader());
        ClassLoader classLoader = CustomeLoad.class.getClassLoader();
        ClassLoader extClassloader = classLoader.getParent();
        System.out.println("current:" + classLoader);
        System.out.println("extloader:"  +extClassloader);
        ClassLoader bootstrap = extClassloader.getParent();
        System.out.println("bootstrap:"  + bootstrap);
        if(bootstrap!=null) {
            System.out.println("bootstrap+parnt:" + bootstrap.getParent());
        } else {
            System.out.println("bootstrap+parnt: null");

        }
        System.out.println("before current thread context loader:"  +Thread.currentThread().getContextClassLoader());
        //设置当前线程上下文加载器为ext
        Thread.currentThread().setContextClassLoader(extClassloader);
        ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
        Iterator<Driver> iterator = loader.iterator();
        while (iterator.hasNext()) {
            Driver driver = (Driver) iterator.next();
            System.out.println("driver:" + driver.getClass() + ",loader:" + driver.getClass().getClassLoader());
        }
        System.out.println("after current thread context loader:"  +Thread.currentThread().getContextClassLoader());
        System.out.println("current loder:" + CustomeLoad.class.getClassLoader());
        System.out.println("ServiceLoader loader:" + ServiceLoader.class.getClassLoader());
    }
    public static void method01(ClassLoader contextClassLoader){
        System.out.println(contextClassLoader);
        while (contextClassLoader!=null){
            contextClassLoader = contextClassLoader.getParent();
            System.out.println(contextClassLoader);
        }
    }
}
